package com.rain.shoppingscienceproj.ui.activity.shopping;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;

import com.rain.shoppingscienceproj.R;
import com.rain.shoppingscienceproj.http.BaseSubscriber;
import com.rain.shoppingscienceproj.http.ExceptionHandle;
import com.rain.shoppingscienceproj.http.RetrofitClient;
import com.rain.shoppingscienceproj.module.Comment;
import com.rain.shoppingscienceproj.ui.activity.BaseActivity;
import com.rain.shoppingscienceproj.ui.adapter.CommentAdapter;
import com.rain.shoppingscienceproj.ui.utile.TitleUtils;
import com.rain.shoppingscienceproj.utils.AppManager;
import com.rain.shoppingscienceproj.utils.LoadingDialog;
import com.rain.shoppingscienceproj.utils.toast.ToastUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class CommentDetailActivity extends BaseActivity {

    @BindView(R.id.rv_list)
    RecyclerView mRecyclerView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    String provinceId;
    @BindView(R.id.ll_no_data)
    LinearLayout ll_no_data;
    private int page = 1;
    private CommentAdapter commentAdapter;
    private List<Comment> commentslsit = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_comment_detail);
        AppManager.getAppManager().addActivity(this);
        ButterKnife.bind(this);
        TitleUtils.TitleView(this, "评论列表");
        provinceId = getIntent().getStringExtra("type");
        loadingDialog = new LoadingDialog(this, "正在加载...");
        initView();
        initData();
        initListener();
    }

    private void initView() {
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        commentAdapter = new CommentAdapter(this);
        mRecyclerView.setAdapter(commentAdapter);
        refreshLayout.setEnableRefresh(true);
        refreshLayout.setEnableLoadmore(true);
    }

    private void initData() {
        loadingDialog.show();
        RetrofitClient.getInstance(this)
                .createBaseApi()
                .commentList(provinceId, page + "", new BaseSubscriber<List<Comment>>(this) {
                    @Override
                    public void onError(ExceptionHandle.ResponeThrowable e) {
                        ToastUtils.showShortToast(e.Message);
                        loadingDialog.dismiss();
                        refreshLayout.setVisibility(View.GONE);
                        ll_no_data.setVisibility(View.VISIBLE);
                    }

                    @Override
                    public void onNext(List<Comment> comments) {
                        super.onNext(comments);
                        loadingDialog.dismiss();
                        if (page == 1) {
                            if (comments.size() == 0) {
                                ll_no_data.setVisibility(View.VISIBLE);
                                refreshLayout.setVisibility(View.GONE);
                            } else {
                                ll_no_data.setVisibility(View.GONE);
                                refreshLayout.setVisibility(View.VISIBLE);
                            }
                        } else {
                            if (comments.size() == 0) {
                                ToastUtils.showShortToast("没有更多数据了...");
                            }
                            ll_no_data.setVisibility(View.GONE);
                            refreshLayout.setVisibility(View.VISIBLE);
                        }
                        commentslsit.addAll(comments);
                        commentAdapter.setList(commentslsit);
                    }
                });

    }

    private void initListener() {
        refreshLayout.setOnRefreshListener(refreshlayout -> refresh());
        refreshLayout.setOnLoadmoreListener(refreshlayout -> load());
    }

    private void load() {
        page++;
        initData();
        refreshLayout.finishLoadmore();
    }

    private void refresh() {
        page = 1;
        commentslsit = new ArrayList<>();
        initData();
        refreshLayout.finishRefresh();

    }
}
